package com.cdie.myschool.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdie.myschool.domain.entity.Calendar;
import com.cdie.myschool.domain.entity.LibraryPassRecord;
import com.cdie.myschool.domain.entity.LibraryPassRecordOld;
import com.cdie.myschool.mapper.CalendarMapper;
import com.cdie.myschool.mapper.StudentTermReportMapper.LibraryPassRecordMapper;
import com.cdie.myschool.mapper.StudentTermReportMapper.LibraryPassRecordOldMapper;
import com.cdie.myschool.service.LibraryPassRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



@Service
@Slf4j
public class LibraryPassRecordServiceImpl extends ServiceImpl<LibraryPassRecordMapper, LibraryPassRecord> implements LibraryPassRecordService{

    private LibraryPassRecordOld restLibraryRecord;

    @Autowired
    private LibraryPassRecordOldMapper libraryPassRecordOldMapper;

    @Autowired
    private LibraryPassRecordMapper libraryPassRecordMapper;


    @Autowired
    private CalendarMapper calendarMapper;


    @Override
    public void importData() {
        int size = 1000;
        importLibraryPassRecord();

    }

    @Override
    public boolean importLibraryPassRecord() {
        int currentPage = 1;
        int pageSize = 10;
        Page<LibraryPassRecordOld> page = null;
        Page<LibraryPassRecordOld> libraryPassThroughRecordPage = null;
        while (true) {
            page = new Page<>(currentPage++, pageSize);
            LambdaQueryWrapper<LibraryPassRecordOld> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderBy(true, true, LibraryPassRecordOld::getRybm)
                    .orderBy(true, true, LibraryPassRecordOld::getTxsj);

            libraryPassThroughRecordPage = libraryPassRecordOldMapper.selectPage(page, queryWrapper);

            List<LibraryPassRecordOld> libraryPassRecordOldList = libraryPassThroughRecordPage.getRecords();
            if (restLibraryRecord != null) {
                libraryPassRecordOldList.add(0, restLibraryRecord);
                restLibraryRecord = null;
            }
            if (!CollectionUtils.isEmpty(libraryPassRecordOldList)) {
                List<LibraryPassRecord> target = calculateLibraryStayTime(libraryPassRecordOldList);
                target.forEach((item) -> {
                    item.setSemesterId((long) getCurrentSchoolYearSemester(new Date()).getSemesterId());
                    //插入到目标数据源
                    libraryPassRecordMapper.insert(item);
                });
                log.info("LibraryPassThroughRecord:执行数据导入:第{}页数据导入成功", libraryPassThroughRecordPage.getCurrent());
            } else {
                log.info("LibraryPassThroughRecord:执行数据导入:第{}页数据为空,停止导入", libraryPassThroughRecordPage.getCurrent());
                break;
            }
        }
        log.info("BookLendingHistory:执行数据导入完成,总计导入{}条数据", libraryPassThroughRecordPage.getTotal());
        return true;
    }

    public List<LibraryPassRecord> calculateLibraryStayTime(List<LibraryPassRecordOld> sourceList) {
        List<LibraryPassRecord> targetList = new ArrayList<>();
        int index = 0;
        for (; index < sourceList.size() - 1; index++) {

            //判断相邻两条记录是否有效
            Long minuteInterval = sourceList.get(index).calculateMinuteInterval(sourceList.get(index + 1));
            if (minuteInterval > 0) {
                LibraryPassRecord libraryPassThroughRecord = new LibraryPassRecord(sourceList.get(index));
                LibraryPassRecord libraryPassThroughRecord2 = new LibraryPassRecord(sourceList.get(index + 1));
                targetList.add(libraryPassThroughRecord.setStayTimeForOutRecord(libraryPassThroughRecord2, minuteInterval));
                index++;
            }
        }
        if (!CollectionUtil.getLast(sourceList).getId().equals(CollectionUtil.getLast(targetList).getUniqueId())) {
            restLibraryRecord = CollectionUtil.getLast(sourceList);
            log.info("当前页最后一条数据没有被导入,加入到下一页头部");
        }
        return targetList;
    }


    /**
     * 获取传入时间对应的学年学期(校历记录)
     *
     * @param date 当前时间
     * @return 校历记录
     */
    public Calendar getCurrentSchoolYearSemester(Date date) {
        return calendarMapper.selectSchoolCalendarByDate(date);
    }


}



















